﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EngineOfEvermore.Common;
using System.Drawing;

namespace EngineOfEvermore.Imaging
{
    public delegate void CameraPositionChangedDelegate( Camera camera, Vector2d oldPosition, Vector2d newPosition );
    public delegate void CameraScaleChangedDelegate( Camera camera, float oldScale, float newScale );
    public delegate void CameraViewportSizeChangedDelegate( Camera camera, Size oldSize, Size newSize );

    public interface IReadonlyCamera
    {
        event CameraPositionChangedDelegate PositionChanged;
        event CameraScaleChangedDelegate ScaleChanged;
        event CameraViewportSizeChangedDelegate ViewPortChanged;

        Vector2d Position { get; }
        float Scale { get; }
        Size ViewportSize { get; }

        RectangleF GetDisplayedArea();
        Vector2d ToScreen( Vector2d point );
        RectangleF ToScreen( RectangleF rect );

        Vector2d ToWorld( Vector2d screenPosition );
        RectangleF ToWorld( RectangleF screenArea );
    }

    public class Camera : IReadonlyCamera
    {
        Vector2d _position;
        float _scale = 1;
        Size _viewportSize = new Size( 640, 480 );
        
        public event CameraPositionChangedDelegate PositionChanged;
        public event CameraScaleChangedDelegate ScaleChanged;
        public event CameraViewportSizeChangedDelegate ViewPortChanged;

        public virtual void ApplyTransform()
        {
            throw new NotSupportedException( "This method must be implemented in a subclass" );
        }

        public Vector2d Position
        {
            get { return _position; }
            set
            {
                if ( _position != value )
                {
                    Vector2d oldPosition = _position;

                    _position = value;
                    
                    if ( PositionChanged != null )
                    {
                        PositionChanged( this, oldPosition, _position );
                    }
                }
            }
        }

        public float Scale
        {
            get { return _scale; }
            set
            {
                if ( _scale != value )
                {
                    float oldScale = _scale;

                    _scale = value;
                    
                    if ( ScaleChanged != null )
                    {
                        ScaleChanged( this, oldScale, _scale );
                    }
                }
            }
        }

        public Size ViewportSize
        {
            get { return _viewportSize; }
            set
            {
                if ( _viewportSize != value )
                {
                    Size oldSize = _viewportSize;

                    _viewportSize = value;
                    
                    if ( ViewPortChanged != null )
                    {
                        ViewPortChanged( this, oldSize, _viewportSize );
                    }
                }
            }
        }

        public RectangleF GetDisplayedArea()
        {
            float scaledViewportWidth = _viewportSize.Width / _scale;
            float scaledViewportHeight = _viewportSize.Height / _scale;

            RectangleF displayedArea = new RectangleF(
                _position.X - scaledViewportWidth * 0.5f,
                _position.Y - scaledViewportHeight * 0.5f,
                scaledViewportWidth,
                scaledViewportHeight
                );

            return displayedArea;
        }

        public Vector2d ToScreen( Vector2d point )
        {
            point -= _position;
            point.X *= _scale;
            point.Y *= _scale;

            return point;
        }

        public RectangleF ToScreen( RectangleF rect )
        {
            rect.X -= _position.X;
            rect.Y -= _position.Y;

            rect.X *= _scale;
            rect.Y *= _scale;
            rect.Width *= _scale;
            rect.Height *= _scale;

            return rect;
        }

        public Vector2d ToWorld( Vector2d screenPosition )
        {
            Vector2d viewPortUpperLeft = _position - ScaledViewPortSize * 0.5f;
            Vector2d worldPosition = ( screenPosition / _scale ) + viewPortUpperLeft;
            
            return worldPosition;
        }

        public RectangleF ToWorld( RectangleF screenArea )
        {
            var upperLeft = this.ToWorld( new Vector2d( screenArea.X, screenArea.Y ) );

            return new RectangleF( upperLeft.X, upperLeft.Y, screenArea.Width / _scale, 
                screenArea.Height / _scale );
        }

        private Vector2d ScaledViewPortSize
        {
            get
            {
                return new Vector2d( _viewportSize.Width / _scale, _viewportSize.Height / _scale );
            }
        }
    }

    #if ENGINE_OF_EVERMORE_ENABLE_UNIT_TESTS
    namespace Test
    {
        using NUnit.Framework;
        
        [TestFixture]
        public class CameraTest
        {
            [Test]
            public void TestToWorld_Point_Simple()
            {
                Camera camera = new Camera();

                camera.ViewportSize = new Size( 20, 10 );
                camera.Position = Vector2d.Zero;
                camera.Scale = 1;

                Vector2d[][] PAIRS = new Vector2d[][] {
                    new Vector2d[] { new Vector2d( 0, 0 ), new Vector2d( -10, -5 ) }, // upper left
                    new Vector2d[] { new Vector2d( 20, 0 ), new Vector2d( 10, -5 ) }, // upper right
                    new Vector2d[] { new Vector2d( 20, 10 ), new Vector2d( 10, 5 ) }, // bottom right
                    new Vector2d[] { new Vector2d( 0, 10 ), new Vector2d( -10, 5 ) }, // bottom left
                    new Vector2d[] { new Vector2d( 10, 5 ), new Vector2d( 0, 0 ) }, // center
                    };

                foreach ( Vector2d[] pair in PAIRS )
                {
                    Assert.IsTrue( camera.ToWorld( pair[0] ).EpsilonEquals( pair[1] ) );
                }
            }

            [Test]
            public void TestToWorld_Point_Scaled()
            {
                Camera camera = new Camera();

                camera.ViewportSize = new Size( 20, 10 );
                camera.Position = Vector2d.Zero;
                camera.Scale = 0.5f;

                Vector2d[][] PAIRS = new Vector2d[][] {
                    new Vector2d[] { new Vector2d( 0, 0 ), new Vector2d( -20, -10 ) }, // upper left
                    new Vector2d[] { new Vector2d( 20, 0 ), new Vector2d( 20, -10 ) }, // upper right
                    new Vector2d[] { new Vector2d( 20, 10 ), new Vector2d( 20, 10 ) }, // bottom right
                    new Vector2d[] { new Vector2d( 0, 10 ), new Vector2d( -20, 10 ) }, // bottom left
                    new Vector2d[] { new Vector2d( 10, 5 ), new Vector2d( 0, 0 ) }, // center
                    };

                foreach ( Vector2d[] pair in PAIRS )
                {
                    Assert.IsTrue( camera.ToWorld( pair[0] ).EpsilonEquals( pair[1] ) );
                }
            }

            [Test]
            public void TestToWorld_Point_PositionOffset()
            {
                Camera camera = new Camera();

                camera.ViewportSize = new Size( 20, 10 );
                camera.Position = new Vector2d( 100, 50 );
                camera.Scale = 1;

                Vector2d[][] PAIRS = new Vector2d[][] {
                    new Vector2d[] { new Vector2d( 0, 0 ), new Vector2d( 90, 45 ) }, // upper left
                    new Vector2d[] { new Vector2d( 20, 0 ), new Vector2d( 110, 45 ) }, // upper right
                    new Vector2d[] { new Vector2d( 20, 10 ), new Vector2d( 110, 55 ) }, // bottom right
                    new Vector2d[] { new Vector2d( 0, 10 ), new Vector2d( 90, 55 ) }, // bottom left
                    new Vector2d[] { new Vector2d( 10, 5 ), new Vector2d( 100, 50 ) }, // center
                    };

                foreach ( Vector2d[] pair in PAIRS )
                {
                    Assert.IsTrue( camera.ToWorld( pair[0] ).EpsilonEquals( pair[1] ) );
                }
            }

            [Test]
            public void TestToWorld_Point_PositionOffset_Scaled()
            {
                Camera camera = new Camera();

                camera.ViewportSize = new Size( 20, 10 );
                camera.Position = new Vector2d( 100, 50 );
                camera.Scale = 0.5f;

                Vector2d[][] PAIRS = new Vector2d[][] {
                    new Vector2d[] { new Vector2d( 0, 0 ), new Vector2d( 80, 40 ) }, // upper left
                    new Vector2d[] { new Vector2d( 20, 0 ), new Vector2d( 120, 40 ) }, // upper right
                    new Vector2d[] { new Vector2d( 20, 10 ), new Vector2d( 120, 60 ) }, // bottom right
                    new Vector2d[] { new Vector2d( 0, 10 ), new Vector2d( 80, 60 ) }, // bottom left
                    new Vector2d[] { new Vector2d( 10, 5 ), new Vector2d( 100, 50 ) }, // center
                    };

                foreach ( Vector2d[] pair in PAIRS )
                {
                    Assert.IsTrue( camera.ToWorld( pair[0] ).EpsilonEquals( pair[1] ) );
                }
            }

            [Test]
            public void TestRectToWorldSimple()
            {
                Camera camera = new Camera();
                camera.ViewportSize = new Size( 800, 600 );
                camera.Position = Vector2d.Zero;
                camera.Scale = 1;

                RectangleF screenRect = new RectangleF( 0, 0, 10, 10 );
                RectangleF expectedWorldRect = new RectangleF( -400, -300, 10, 10 );

                Assert.That( camera.ToWorld( screenRect ), Is.EqualTo( expectedWorldRect ) );
            }

            [Test]
            public void TestRectToWorldCameraScaled()
            {
                Camera camera = new Camera();
                camera.ViewportSize = new Size( 800, 600 );
                camera.Position = Vector2d.Zero;
                camera.Scale = 2;

                RectangleF screenRect = new RectangleF( 0, 0, 10, 10 );
                RectangleF expectedWorldRect = new RectangleF( -200, -150, 5, 5 );

                Assert.That( camera.ToWorld( screenRect ), Is.EqualTo( expectedWorldRect ) );
            }

            [Test]
            public void TestRectToWorldCameraMoved()
            {
                Camera camera = new Camera();
                camera.ViewportSize = new Size( 800, 600 );
                camera.Position = new Vector2d( 100, 100 );
                camera.Scale = 1;

                RectangleF screenRect = new RectangleF( 0, 0, 10, 10 );
                RectangleF expectedWorldRect = new RectangleF( -300, -200, 10, 10 );

                Assert.That( camera.ToWorld( screenRect ), Is.EqualTo( expectedWorldRect ) );
            }

            [Test]
            public void TestRectToWorldCameraMovedAndScaled()
            {
                Camera camera = new Camera();
                camera.ViewportSize = new Size( 800, 600 );
                camera.Position = new Vector2d( 100, 100 );
                camera.Scale = 2;

                RectangleF screenRect = new RectangleF( 0, 0, 10, 10 );
                RectangleF expectedWorldRect = new RectangleF( -100, -50, 5, 5 );

                Assert.That( camera.ToWorld( screenRect ), Is.EqualTo( expectedWorldRect ) );
            }
        }
    }
    #endif
}
